import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  UseGuards,
} from '@nestjs/common';
import { jwtGuard } from 'src/auth/guard';
import { PrismaService } from 'src/prisma/prisma.service';

@Controller('student')
export class StudentController {
  constructor(private prisma: PrismaService) {}
  @Get('/')
  book(@Query() Param) {
    return this.prisma.student.findMany({
      where: {
        StuId: Param.StuId ? parseInt(Param.StuId) : undefined,
        name: Param.name ? { contains: Param.name } : undefined,
        StuNumber: Param.StuNumber ? { contains: Param.StuNumber } : undefined,
        sex: Param.sex ? { contains: Param.sex } : undefined,
        ZJH: Param.ZJH ? { contains: Param.ZJH } : undefined,
        LXDH: Param.LXDH ? { contains: Param.LXDH } : undefined,
        
      },
    });
  }

  @UseGuards(jwtGuard)
  @Post('/')
  addBook(@Body() data) {
    console.log(data);
    return this.prisma.student.create({
      data: { ...data },
    });
  }

  @UseGuards(jwtGuard)
  @Put('/:id')
  changeBook(@Body() data, @Param() param) {
    console.log(data);
    return this.prisma.student.update({
      where: {
        StuId: parseInt(param.id),
      },
      data: { ...data },
    });
  }

  @UseGuards(jwtGuard)
  @Delete('/:id')
  async deleteBook(@Param() param) {
    console.log(param);
    return await this.prisma.student.delete({
      where: {
        StuId: parseInt(param.id),
      },
    });
  }
}
